package StudyCode.summary.myfile;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.zip.DataFormatException;
import java.util.zip.Inflater;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class DecompressByteArray {
	public static void main(String[] argv) throws Exception {
		byte[] compressedData = new byte[]{1, 2, 31, 3, 23, 23, 42};
		System.out.println(Arrays.toString(decompress(compressedData)));
	}

	private static byte[] decompress(byte[] compressedData, int len) throws IOException, DataFormatException {
		Inflater decompressor = new Inflater();
		decompressor.setInput(compressedData, 0, len);

		ByteArrayOutputStream bos = new ByteArrayOutputStream(
				compressedData.length);

		byte[] buf = new byte[1024];
		while (!decompressor.finished()) {
			int count = decompressor.inflate(buf);
			bos.write(buf, 0, count);
		}
		bos.close();
		return bos.toByteArray();
	}

	private static byte[] decompress(byte[] compressedData) throws IOException, DataFormatException {
		return decompress(compressedData, compressedData.length);
	}

}
